﻿<!-- 
<$@ Options Version="3"$>
<$@ Options LaunchPage="true"$>
 <$@ Options Name="Silverlight 3 Diagnostic"$>
<$@ Parameter Name="AutoPlay" Caption="Automatically start video when cued" Tooltip="Select to begin playback when video is loaded" Type="System.Boolean" Default="True" $>
<$@ Parameter Name="AutoLoad" Caption="Automatically cue video when page is loaded" Tooltip="Select to begin downloading video once page is loaded" Type="System.Boolean" Default="True" $>
<$@ Parameter Name="EnableCaptions" Caption="Allow closed captions to show" Tooltip="Select to allow closed caption text over video during playback" Type="System.Boolean" Default="True" $>
<$@ Parameter Name="Muted" Caption="Mute player on start" Tooltip="Select to begin playback in a muted state" Type="System.Boolean" Default="False" $>
<$@ Parameter Name="AllowedCodecs" Caption="Allowed Codecs" Tooltip="Codecs template supports" Readonly="true" Type="System.String" Default="H264,AAC,AC3,VC1,WMA,WMV1,WMV2,WMV3,WMAPRO,WMAPROFESSIONAL," $>
<$@ Parameter Name="DisplayTimeCode" Caption="Display Timecode" Tooltip="Select to display the position and duration fields as SMPTE timecode" Type="System.Boolean" Default="False" $>
<$@ Parameter Name="EnableCachedComposition" Caption="Enable Cached Composition" Tooltip="Select to use video hardware to speed up player rendering" Type="System.Boolean" Default="True" $>
<$@ Parameter Name="EnablePopOut" Caption="Enable player popout" Tooltip="Select to allow the media to play in its own window" Type="System.Boolean" Default="True" $>
<$@ Parameter Name="EnableOffline" Caption="Enable player to be run offline" Tooltip="Select to allow player to playback offline" Type="System.Boolean" Default="True" $>
<$@ Parameter Name="StretchMode" Caption="Stretch Mode" Tooltip="Change the way the template is displayed on the page" Type="System.Enum" EnumValues="None=0,Uniform=1,Uniform To Fill=2,Fill=3" Default="None" $>
<$@ Capabilities Value="silverlight30,audio,video,captions,chapters,gallery,supports16by9,supports4by3,webbrowsable,adaptivestreaming" $>
<$@ ScriptCommands Value="caption" $>
<$@ IgnoreDirectory Value="Source" $>

<$@ Import Namespace="System.IO" $>
<script runat="authoring">

    string Esc(string value)
    {                
        if(string.IsNullOrEmpty(value))
        {
            return "";
        }
        value = System.Security.SecurityElement.Escape(value);    
        value = System.Uri.EscapeDataString(value);
        return value;
    }

    int indentLevel = 0;

    void IndentBy(int by)
    {
        indentLevel += by;
    }
        
    void Indent()
    {
        indentLevel++;
    }
    
    void Outdent()
    {
        indentLevel--;
    } 
    
    string StretchModeString(string val)
    {
        return "None,Uniform,UniformToFill,Fill".Split(',')[Convert.ToInt32(val)];
    }
                      
    string IndentString()
    {
        string tmp = "\r\n";
        for (int i=0;i<indentLevel;i++)
        {
            tmp += "\t";
        }
        return tmp;
    }                
            
    string ChapterImageXML(PublishedMarker marker)
    {
        if (String.IsNullOrEmpty(marker.ThumbnailPath))
        {
            return "";
        }
        return "<ThumbSource>"+Esc(marker.ThumbnailPath)+"</ThumbSource>";
    }
    
    long TotalFileSize(PublishedItem item)
    {
        long totalSize=0;
        foreach(long size in item.OutputFileSizes)
            totalSize+=size;
        return totalSize;
    }

    string ThumbnailImageFilename()
    {
        for(int iItem=0;iItem<PublishedItems.Length;iItem++)
        {
            if (PublishedItems[iItem].ThumbnailPath!=null && PublishedItems[iItem].ThumbnailPath.Length>0)
                return PublishedItems[iItem].ThumbnailPath;
        }
        return "";
    }
    
    string PlayListParam(string strAllowedCodecs)
    {
        IndentBy(8);
        StringBuilder result = new StringBuilder();
        for (int iItem = 0; iItem < PublishedItems.Length; iItem++)
        {
            MediaItem mi = PublishedItems[iItem].MediaItem;
            string mediaUrl = PublishedItems[iItem].OutputFileName;
            if (mi.OutputFormat.VideoProfile != null && mi.OutputFormat.VideoProfile.SmoothStreaming)
            {
                mediaUrl += "\\Manifest";
            }

            string title = mi.Metadata[MetadataNames.Title];
            if (string.IsNullOrEmpty(title) || title.Length < 1)
            {
                title = Path.GetFileNameWithoutExtension(PublishedItems[iItem].OutputFileName);
            }

            result.Append(IndentString() + "<PlaylistItem>");
            Indent();
            result.Append(IndentString() + "<AudioCodec>" + (mi.OutputFormat.AudioProfile == null ? string.Empty : Esc(mi.OutputFormat.AudioProfile.Codec.ToString())) + "</AudioCodec>"
                        + IndentString() + "<Description>" + Esc(mi.Metadata[MetadataNames.Description]) + "</Description>"
                        + IndentString() + "<FileSize>" + TotalFileSize(PublishedItems[iItem]) + "</FileSize>"
                        + IndentString() + "<FrameRate>" + mi.OutputFrameRate.ToString(System.Globalization.CultureInfo.InvariantCulture) + "</FrameRate>"
                        + IndentString() + "<Height>" + mi.VideoSize.Height.ToString(System.Globalization.CultureInfo.InvariantCulture) + "</Height>"
                        + IndentString() + "<IsAdaptiveStreaming>" + (mi.OutputFormat.VideoProfile != null && mi.OutputFormat.VideoProfile.SmoothStreaming).ToString().ToLower() + "</IsAdaptiveStreaming>"
                        + IndentString() + "<MediaSource>" + Esc(mediaUrl) + "</MediaSource>"
                        + IndentString() + "<ThumbSource>" + Esc(PublishedItems[iItem].ThumbnailPath) + "</ThumbSource>"
                        + IndentString() + "<Title>" + Esc(title) + "</Title>"
                        + IndentString() + "<VideoCodec>" + (mi.OutputFormat.VideoProfile == null ? string.Empty : Esc(mi.OutputFormat.VideoProfile.Codec.ToString())) + "</VideoCodec>"
                        + IndentString() + "<Width>" + mi.VideoSize.Width.ToString(System.Globalization.CultureInfo.InvariantCulture) + "</Width>");

            if (PublishedItems[iItem].Markers.Count > 0)
            {
                result.Append( IndentString() + "<Chapters>" );
                Indent();
                for (int iMarker = 0; iMarker < PublishedItems[iItem].Markers.Count; iMarker++)
                {
                    PublishedMarker marker = PublishedItems[iItem].Markers[iMarker];
                    result.Append( IndentString() + "<ChapterItem>");
                    Indent();
                    result.Append( IndentString() + "<Position>" + marker.Time.TotalSeconds.ToString("F3", System.Globalization.CultureInfo.InvariantCulture) + "</Position>");
                    result.Append( IndentString() + ChapterImageXML(marker));
                    result.Append( IndentString() + "<Title>" + Esc(marker.Value) + "</Title>");
                    Outdent();
                    result.Append( IndentString() + "</ChapterItem>");
                }
                Outdent();
                result.Append( IndentString() + "</Chapters>");
            }
            Outdent();
            result.Append( IndentString() + "</PlaylistItem>");
        }
        IndentBy(-8);
        return result.ToString();
    }        
</script> 
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<!-- saved from url=(0014)about:internet -->
<head>
    <style type="text/css">
    html, body {
        height: 100%;
        overflow: auto;
    }
    body {
        padding: 0;
        margin: 0;
    }
    .fadeSome {
        opacity:0.30;
        filter:alpha(opacity=30);
        -moz-opacity: 30%;        
    }
    .fadeLots {
        opacity:0.50;
        filter:alpha(opacity=50);
        -moz-opacity: 0.5;        
    }  
    .fadeCompletely {
        opacity:0.0;
        filter:alpha(opacity=0);
        -moz-opacity: 0.0;
    }     
    #silverlightControlHost {
        height: 100%;
    }
    </style>
    
    <script type="text/javascript">
        function onSilverlightError(sender, args) {

            var appSource = "";
            if (sender != null && sender != 0) {
                appSource = sender.getHost().Source;
            }
            var errorType = args.ErrorType;
            var iErrorCode = args.ErrorCode;

            var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n";

            errMsg += "Code: " + iErrorCode + "    \n";
            errMsg += "Category: " + errorType + "       \n";
            errMsg += "Message: " + args.ErrorMessage + "     \n";

            if (errorType == "ParserError") {
                errMsg += "File: " + args.xamlFile + "     \n";
                errMsg += "Line: " + args.lineNumber + "     \n";
                errMsg += "Position: " + args.charPosition + "     \n";
            }
            else if (errorType == "RuntimeError") {
                if (args.lineNumber != 0) {
                    errMsg += "Line: " + args.lineNumber + "     \n";
                    errMsg += "Position: " + args.charPosition + "     \n";
                }
                errMsg += "MethodName: " + args.methodName + "     \n";
            }

            throw new Error(errMsg);
        }

        function highlightDownloadArea(fOn) {
            document.getElementById("overlay").className = (fOn) ? "fadeSome" : "fadeLots";            
        }

	function CloseWindow()
	{
	    window.close();
	}

        function DbgOut(value) {
            document.getElementById("dbgOut").innerHTML = value + document.getElementById("dbgOut").innerHTML;
        }

        function PlayerControl() {
            return document.getElementById("player").Content.Player;
        }

        function ChangeVol(inc) {
            PlayerControl().Volume = PlayerControl().Volume + inc;
            DbgOut("Volume=" + PlayerControl().Volume + "<br>");
        }

        function MediaStateChanged(sender, mediaState) {
            switch (PlayerControl().CurrentState) {
                case 0: DbgOut("MediaState=Closed<br>"); break;
                case 1: DbgOut("MediaState=Opening<br>"); break;
                case 2: DbgOut("MediaState=Buffering<br>"); break;
                case 3: DbgOut("MediaState=Playing<br>"); break;
                case 4: DbgOut("MediaState=Paused<br>"); break;
                case 5: DbgOut("MediaState=Stopped<br>"); break;
                case 6: DbgOut("MediaState=Individualizing<br>"); break;
                case 7: DbgOut("MediaState=AcquiringLicense<br>"); break;
            }
        }

        function MarkerReached(sender, markerInfo) {
            DbgOut("MARKER Time=" + markerInfo.Marker.Time + " Type=" + markerInfo.Marker.Type + " Text=" + markerInfo.Marker.Text + "<br>");
        }

        function OnEverySecond() {
            window.status = PlayerControl().Position;
        }

       function AddPlaylistItemByXML() {		
        	var xmlPlaylistItem =	'<Playlist>'+
					'		<AutoPlay>true</AutoPlay>'+
					'		<AutoLoad>true</AutoLoad>'+
					'		<StartMuted>false</StartMuted>'+
					'		<DisplayTimeCode>false</DisplayTimeCode>'+
					'		<EnableCaptions>true</EnableCaptions>'+
					'		<StretchMode>None</StretchMode>'+
					'		<EnableCachedComposition>true</EnableCachedComposition>'+
					'		<Items>'+
					'			<PlaylistItem>'+
					'				<AudioCodec>Wma</AudioCodec>'+
					'				<Description></Description>'+
					'				<FileSize>557167</FileSize>'+
					'				<FrameRate>23.9760431376968</FrameRate>'+
					'				<Height>288</Height>'+
					'				<IsAdaptiveStreaming>false</IsAdaptiveStreaming>'+
					'				<MediaSource>Pinball.wmv</MediaSource>'+
					'				<ThumbSource>Pinball_Thumb.jpg</ThumbSource>'+
					'				<Title>This%20is%20the%20Metadata%20Title</Title>'+
					'				<VideoCodec>VC1</VideoCodec>'+
					'				<Width>512</Width>'+
					'				<Chapters>'+
					'					<ChapterItem>'+
					'						<Position>1.964</Position>'+
					'						<Title>Marker1</Title>'+
					'					</ChapterItem>'+
					'					<ChapterItem>'+
					'						<Position>6.334</Position>'+
					'						<Title>Marker2</Title>'+
					'					</ChapterItem>'+
					'				</Chapters>'+
					'			</PlaylistItem>'+
					'		</Items>'+
					'	</Playlist>';
        	PlayerControl().Playlist.ParseXml(xmlPlaylistItem);       
        }

        function AddPlaylistItemProgrammatically() {
            var PlaylistItem = PlayerControl().Playlist.CreateNewPlaylistItem();
            PlaylistItem.Title = "New Playlist Item";
            PlaylistItem.Description = "Description would go here";
            PlaylistItem.MediaSource = PlayerControl().CreateUri("http://server/sample.WMV")

            var ChapterItem = PlaylistItem.CreateNewChapterItem();
            ChapterItem.Title = "My new chapter";
            ChapterItem.Position = 2.0;
            ChapterItem.ThumbSource = null;
            PlaylistItem.Chapters.Add(ChapterItem);
            PlayerControl().Playlist.Items.Add(PlaylistItem);
        }

        function DumpPlaylistItem(playlistItem) {
            DbgOut("MediaSource = " + playlistItem.MediaSource+"<br>");
            DbgOut("Width = " + playlistItem.Width + "<br>");
            DbgOut("Height = " + playlistItem.Height + "<br>");
        }

        function DumpPlaylist(playlist) {
            DbgOut("Items in playlist = " + playlist.Count + "<br>");
            for (var iItem = 0; iItem < playlist.Count; iItem++)
                DumpPlaylistItem(playlist[iItem]);
        }

        function ManipulatePlaylist() {
            // misc manipulations of playlist
            AddPlaylistItemByXML();
            // remove 1st item in playlist
            AddPlaylistItemProgrammatically();
            PlayerControl().Playlist.RemoveAt(1);
            DumpPlaylist(PlayerControl().Playlist);
        }

        function onSilverlightLoaded() {
            DbgOut("Control loaded<br>");
            PlayerControl().addEventListener("StateChanged", MediaStateChanged);
            PlayerControl().addEventListener("MarkerReached", MarkerReached);
            // ManipulatePlaylist();            
            window.setInterval(OnEverySecond, 1000);
        }                
    </script>
</head>

<body>
    <!-- Runtime errors from Silverlight will be displayed here.
	This will contain debugging information and should be removed or hidden when debugging is completed -->
	<div id='errorLocation' style="font-size: small;color: Gray;"></div>

    <div id="silverlightControlHost">
        <object id="player" data="data:application/x-silverlight," type="application/x-silverlight" width="60%" height="60%">
			<param name="source" value="MediaPlayerTemplate.xap"/>
			<param name="onerror" value="onSilverlightError" />
            		<param name="onload" value="onSilverlightLoaded" />
			<param name="autoUpgrade" value="true" />
			<param name="enableHtmlAccess" value="true" />
			<param name="enableGPUAcceleration" value="true" />			
            <param name="minRuntimeVersion" value="3.0.40624.0" />            
			<param name="initparams" value='playerSettings = 
						<Playlist>
							<AutoPlay><$=TemplateParameter.AutoPlay.ToString().ToLower()$></AutoPlay>
							<AutoLoad><$=TemplateParameter.AutoLoad.ToString().ToLower()$></AutoLoad>
							<StartMuted><$=TemplateParameter.Muted.ToString().ToLower()$></StartMuted>
							<DisplayTimeCode><$=TemplateParameter.DisplayTimeCode.ToString().ToLower()$></DisplayTimeCode>
							<EnableCaptions><$=TemplateParameter.EnableCaptions.ToString().ToLower()$></EnableCaptions>
							<StretchMode><$=StretchModeString(TemplateParameter.StretchMode)$></StretchMode>
							<EnableCachedComposition><$=TemplateParameter.EnableCachedComposition.ToString().ToLower()$></EnableCachedComposition>														
							<Items><$=PlayListParam(TemplateParameter.AllowedCodecs)$>
							</Items>
						</Playlist>'/>	
    		<!--  unused valid parameters
    		<param name="enableFrameRateCounter" value="bool" />    		
    		<param name="enableRedrawRegions" value="bool" />
    		<param name="maxFrameRate" value="int" />
    		<param name="allowHtmlPopupWindow" value="bool"/>
    		<param name="background" value="colorValue"/>
    		<param name="splashScreenSource" value="uri"/>
    		<param name="fullScreen" value="bool"/>
    		<param name="onFullScreenChanged" value="functionname"/>
    		<param name="onResize" value="functionname"/>
    		<param name="onSourceDownloadComplete" value="functionname"/>
    		<param name="onSourceDownloadProgressChanged" value="functionname"/>
	        <param name="windowLess" value="bool"/>
     		-->				
             <div onmouseover="highlightDownloadArea(true)" onmouseout="highlightDownloadArea(false)">
                    <img src="<$=ThumbnailImageFilename()$>" style="position:absolute;width:100%;height:100%;border-style:none;" onerror="this.style.display='none'"/>
                    <img src="Preview.png" style="position:absolute;width:100%;height:100%;border-style:none;" onerror="this.style.display='none'"/>                                            
                    <div id="overlay" class="fadeLots" style="position:absolute;width:100%;height:100%;border-style:none;background-color:white;"/></div>
                    <table width="100%" height="100%" style="position:absolute;"><tr><td align="center" valign="middle">                       
                    <img src="http://go2.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"> 
                    </td></tr></table>                   
                    <a href="http://go2.microsoft.com/fwlink/?LinkID=124807">
                        <img src="" class="fadeCompletely" style="position:absolute;width:100%;height:100%;border-style:none;" alt="Get Microsoft Silverlight"/>
                    </a>                   
             </div>   
		</object>
		<iframe id='_sl_historyFrame' style='visibility:hidden;height:0;width:0;border:0px'></iframe>
		<div id="ScriptedControls" style="background-color:WindowFrame">
		    <button onclick="PlayerControl().Stop()">Stop</button>
            <button onclick="PlayerControl().Play()">Play</button>
            <button onclick="PlayerControl().Pause()">Pause</button>
            <button onclick="PlayerControl().SeekToPreviousItem()">|&lt;&lt;</button>
            <button onclick="PlayerControl().SeekToNextItem()">&gt;&gt;|</button>
            <button onclick="PlayerControl().ShowChapters(true)">Show Chapters</button>
            <button onclick="PlayerControl().ShowChapters(false)">Hide Chapters</button>
            <button onclick="PlayerControl().ShowPlaylist(true)">Show Playlist</button>
            <button onclick="PlayerControl().ShowPlaylist(false)">Hide Playlist</button><br />        
            <button onclick="ChangeVol(0.1)">VOL+</button>
            <button onclick="ChangeVol(-0.1)">VOL-</button>
            <input id="muteBtn" type="checkbox" onclick="PlayerControl().Mute = muteBtn.checked"/>MUTE
            <input id="time" value="1.0"/>                 
            <button onclick="PlayerControl().Position = time.value">JUMP</button>
            <div id="dbgOut" style="height:100px;background-color:Gray;overflow:auto;">Debug Output<br /></div>        				
        </div>		
    </div>
</body>
</html>
